package com.alibaba.org.service.impl;

import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.org.domain.Department;
import com.alibaba.org.mapper.DepartmentMapper;
import com.alibaba.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /** 添加 */
    @Override
    public void add(Department department) {
        departmentMapper.add(department); // 前端添加数据后，可以获取自增长id
        String dirPath = "";
        // 从前端获取上级部门后设置dirPath
        if (department.getParent_id() == null) { // 没有选择上级部门 + 选择上级部门后清空上级部门
            dirPath = "/" + department.getId();
        } else { // 选择上级部门
            // 根据当前部门的parent_id获取上级部门
            Department parentDept = departmentMapper.findOne(department.getParent_id());
            // 获取上级部门的dirPath
            String parentDirPath = parentDept.getDirPath();
            // 拼接上级部门的dirPath + 当前部门的id
            dirPath = parentDirPath + "/" + department.getId();
        }
        // 将dirPath设置到对象中更新dirPath
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /** 修改 */
    @Override
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent_id() == null) {
            dirPath = "/" + department.getId();
        } else {
            // 根据当前部门的parent_id获取上级部门
            Department parentDept = departmentMapper.findOne(department.getParent_id());
            // 获取上级部门的dirPath
            String parentDirPath = parentDept.getDirPath();
            // 拼接上级部门的dirPath + 当前部门的id
            dirPath = parentDirPath + "/" + department.getId();
        }
        // 将dirPath设置到对象中更新dirPath
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    /**
     * 获取上级部门(部门树)
     * @return
     */
    @Override
    public List<Department> deptTree() {
        // 1. 一次性查询出所有数据，放到内存中，可以使用map
        // 2. 从所有数据中找出第一层级/顶级部门：这就是我们需要返回的数据
        // 3. 遍历所有部门只要不是第一层级的，找自己的上级，并将自己放入上级的children中
        // 将所有的部门装到map中  - 每次获取父部门都从这里获取
        List<Department> departments = departmentMapper.findAll();
        Map<Long, Department> depts = new HashMap<>();
        // 将所有部门及其对应的id保存到map中
        for (Department department : departments) {
            depts.put(department.getId(), department);
        }

        // 前端需要的部门树集合数据
        List<Department> deptTree = new ArrayList<>();
        // 遍历查询到的所有部门
        for (Department department : departments) {
            if (department.getParent_id() == null){ // 如果没有parent_id,则为顶级部门
                deptTree.add(department);
            } else { // 如果有parent_id
                // 则在map中根据parent_id查询上级部门
                Department parentDept = depts.get(department.getParent_id());
                // 将当前部门添加到上级部门的children中
                parentDept.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
